﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Framework.Library.Base.Base;
using Framework.Library.Base.Extensions;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAO;
using System.Linq;
using Framework.Library.Core.Utils.GlobalHelper;
using Framework.Library.Data.DAL.JObject;
using Framework.Library.Core.Config;

namespace Framework.Portal.Layouts.Client.Modules.Right
{
    public partial class EventInfo : BaseControl
    {
        public EventInfo()
        {
        }

        public EventInfo(JSection section)
        {
            if (!string.IsNullOrEmpty(section.parameter))
            {
                string[] arrParams = section.parameter.Split('&');
                if (arrParams.Length > 0)
                    catId = Convert.ToInt32(arrParams[0].Split('=')[1]);
            }
            MENUNAME = section.title;
        }
        public string MENUNAME = string.Empty;

        protected static int catId = 0;
        protected static int curMenu = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                curMenu = string.IsNullOrEmpty(Request.QueryString["m"]) ? 0 : Convert.ToInt32(Request.QueryString["m"]);
                if (curMenu == 0)
                {
                    var infoMenu = ManagerFactory.PortalMenuManager.GetInfo(Configurations.PORTALID, curMenu);
                    if (infoMenu != null)
                        curMenu = infoMenu.ID;
                }

                List<PortalMenu> dtCommands = ManagerFactory.PortalMenuManager.GetByCategory(Configurations.PORTALID, catId).FindAll(x => x.VISIBLE > 0 && x.PARENTID == catId);
                if (dtCommands.Count > 0)
                {
                    rptMenu.DataSource = dtCommands;
                    rptMenu.DataBind();
                }
            }
        }

        protected void rptMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var info = (PortalMenu)e.Item.DataItem;
                //<a href="#
                var hplContent = (Literal)e.Item.FindControl("lblMenu");
                string formatLink = "<a href=\"{0}\"{1}{2}>{3}</a>";
                string curClass = (info.ID == curMenu) ? " class=\"active\"" : string.Empty;
                switch (info.LINKTYPE)
                {
                    case 0:
                        hplContent.Text = string.Format(formatLink, "#", curClass, string.Empty, info.NAME);
                        break;
                    case 1:
                        hplContent.Text = string.Format(formatLink, Configurations.ROOTDIR + "Default.aspx?p=" + info.PORTALID + "&m=" + info.ID, curClass, string.Empty, info.NAME);
                        break;
                    case 2:
                        hplContent.Text = string.Format(formatLink, info.PARAMETTERS.Trim(), " target=\"_blank\" " + curClass, string.Empty, info.NAME);
                        break;
                    case 3:
                        hplContent.Text = string.Format(formatLink, "javascript:;", curClass, "onclick=\"" + info.PARAMETTERS.Trim() + "\"", info.NAME);
                        break;
                    default:
                        hplContent.Text = string.Format(formatLink, Configurations.ROOTDIR + "Default.aspx?p=" + info.PORTALID + "&m=" + info.ID, curClass, string.Empty, info.NAME);
                        break;
                }
            }
        }
    }
}